package com.kalman.common.spider;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Set;

/**
 * @author kalman03
 */
public class BatchWebSpiderListener implements WebSpiderListener {

	private final Set<WebSpiderListener> listeners;

	public BatchWebSpiderListener(Set<WebSpiderListener> listeners) {
		checkNotNull(listeners, "listeners can't be null.");
		this.listeners = listeners;
	}

	@Override
	public void onStarted(WebSpiderParam param) {
		for (WebSpiderListener listener : listeners) {
			listener.onStarted(param);
		}
	}

	@Override
	public void handleWebPage(WebPage webPage) {
		for (WebSpiderListener listener : listeners) {
			listener.handleWebPage(webPage);
		}
	}

	@Override
	public void onEnd(long success, long failed) {
		for (WebSpiderListener listener : listeners) {
			listener.onEnd(success, failed);
		}
	}

	@Override
	public void onException(Throwable t) {
		for (WebSpiderListener listener : listeners) {
			listener.onException(t);
		}
	}

}
